<?php 

/**
 * Class View
 * 
 * Save controller variables
 * Allow access to saved variables from templates
 * Load layout template
 * Load action-view template
 */

define('TEMPLATE_EXTENSION', '.phtml');

class DM_View extends StdClass {
	
	protected $_options = array(
					'module' => 'default',
					'controller' => 'index',
					'action' => 'index',
					'view' => 'index',
					'layout' => 'layout',
					'helpers' => array(),
					'disableLayout' => false,
					'breakOnRender' => true,
                                        'disableRender' => false,
                                        'ajaxOutput' => null
				);
				
	protected $layoutDirectories = array(
					'/{module}/templates'
				);
				
	protected $viewDirectories = array(
					'/{module}/templates/{controller}'
				);
				
	public function __construct( $_options = array() ) {
		$this->_options = array_merge( $this->_options, $_options ); 		
	}
	
	public function _setOptions( $_options = array() ) {
		$this->_options = array_merge( $this->_options, $_options ); 		
	}
	
	public function _render() {
               if ( $this->_options['disableRender'] ) {
                    if ( $this->_options['breakOnRender'] ) {
                        die() or exit();
                    }
                    return;
                }

                if ( $this->_options['breakOnRender'] ) {
			$headers = DM_Application::get()->getHeaders();
			if ( is_array($headers) && count($headers) ) {
				foreach ($headers as $header) {
					header( $header, true ); // @TODO may be error TRUE/FALSE on replace
				}
			}
		}

                if ( !empty($this->_options['ajaxOutput']) ) {
                    echo $this->_options['ajaxOutput'];
                    die() or exit();
                }
                
		if ( is_array($this->_options['helpers']) && count($this->_options['helpers']) ) {
			$viewHelper = DM_ViewHelper::get();			
			foreach( $this->_options['helpers'] as $helper ) {
				$viewHelper->loadHelper( $this, $helper );
			}
		}
		
		if ( !$this->_options['disableLayout'] ) {			
			$this->_loadLayout();
		} else {			
			$this->context();
		}
		
		if ( $this->_options['breakOnRender'] ) {
			DM_Debug::get()->printAll();
			die() or exit(); // ;)
		}
	}
	
	public function loadHelper( $helperName ) {
		$viewHelper = DM_ViewHelper::get();			
		$viewHelper->loadHelper( $this, $helperName );
	}
	
	protected function _loadLayout() {	
		foreach( $this->layoutDirectories as $layoutDir ) {
			$layoutDir = APPLICATION_PATH.str_replace( '{module}', $this->_options['module'], $layoutDir );
			if ( is_file( $layoutDir.'/'.$this->_options['layout'].TEMPLATE_EXTENSION) ) {
				require $layoutDir.'/'.$this->_options['layout'].TEMPLATE_EXTENSION;
				return;
			}
		}
		throw new Exception('Layout '.$this->_options['layout'].TEMPLATE_EXTENSION.' no found in layout directories');
	}
	
	protected function context() {			
			foreach( $this->viewDirectories as $viewDir ) {
				$viewDir = APPLICATION_PATH.str_replace( array('{module}', '{controller}' ), array( $this->_options['module'], $this->_options['controller'] ), $viewDir );
				if ( is_file( $viewDir.'/'.$this->_options['view'].TEMPLATE_EXTENSION) ) {					
					require $viewDir.'/'.$this->_options['view'].TEMPLATE_EXTENSION;
					return;
				}
			}
			DM_Debug::get()->addError( 'View '.$this->_options['view'].TEMPLATE_EXTENSION.' no found in view directory' );
	}
}

?>